package online.xybh.qa.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import online.xybh.qa.dto.QuestionInfoDTO;
import online.xybh.qa.model.Question;
import online.xybh.qa.model.QuestionExample;
import online.xybh.qa.model.Student;
import online.xybh.qa.service.QuestionService;
import online.xybh.qa.utils.InviteCodeGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Resource
    private online.xybh.qa.dao.QuestionMapper questionMapper;
    @Resource
    private online.xybh.qa.dao.StudentMapper studentMapper;
    @Resource
    private online.xybh.qa.dao.QuestionExtMapper questionExtMapper;


    @Override
    public void createQuestion(String title, String content, Integer subject, String author) {
        Question question = new Question();
        question.setQuestionId(InviteCodeGenerator.generateInviteCode());
        question.setTitle(title);
        question.setContent(content);
        question.setSubject(subject);
        question.setAuthor(author);
        question.setType(1);
        question.setCreateTime(new Date());
        question.setModifyTime(question.getCreateTime());
        questionMapper.insert(question);
        Student student = studentMapper.selectByPrimaryKey(author);
        student.setQuestion(student.getQuestion()+1);
        studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    public Question queryQuestionByAllInfo(String title, String content, Integer subject, String author) {
        QuestionExample questionExample = new QuestionExample();
        questionExample.createCriteria()
                .andTitleEqualTo(title)
                .andContentEqualTo(content)
                .andSubjectEqualTo(subject)
                .andAuthorEqualTo(author);
        List<Question> questions = questionMapper.selectByExample(questionExample);
        return questions != null ? questions.get(0) : null;
    }

    @Override
    public QuestionInfoDTO queryQuestionById(String id) {
        Question question = questionMapper.selectByPrimaryKey(id);
        Student student = studentMapper.selectByPrimaryKey(question.getAuthor());
        QuestionInfoDTO questionInfoDTO = new QuestionInfoDTO();
        BeanUtils.copyProperties(question, questionInfoDTO);
        questionInfoDTO.setName(student.getName());
        return questionInfoDTO;
    }

    @Override
    public PageInfo queryChinese(Integer page, Integer size) {
        return queryQuestion(page, size, 1);
    }

    @Override
    public PageInfo queryMath(Integer page, Integer size) {
        return queryQuestion(page, size, 2);
    }

    @Override
    public PageInfo queryEnglish(Integer page, Integer size) {
        return queryQuestion(page, size, 3);
    }

    @Override
    public PageInfo queryProgram(Integer page, Integer size) {
        return queryQuestion(page, size, 4);
    }

    private PageInfo queryQuestion(Integer page, Integer size, Integer subject) {
        Question question = new Question();
        question.setSubject(subject);
        PageHelper.startPage(page, size);
        List<QuestionInfoDTO> questionInfoDTOS = questionExtMapper.selectByExample(question);
        PageInfo info = new PageInfo<>(questionInfoDTOS);
        return info;
    }
}
